home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_src.lha / KSAMPLE / SAM_CLNT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-01  |  4.9 KB  |  199 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/appl/sample/RCS/sample_client.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information,
  8.  * please see the file <mit-copyright.h>.
  9.  *
  10.  * sample_client:
  11.  * A sample Kerberos client, which connects to a server on a remote host,
  12.  * at port "sample" (be sure to define it in /etc/services)
  13.  * and authenticates itself to the server. The server then writes back
  14.  * (in ASCII) the authenticated name.
  15.  *
  16.  * Usage:
  17.  * sample_client <hostname> <checksum>
  18.  *
  19.  * <hostname> is the name of the foreign host to contact.
  20.  *
  21.  * <checksum> is an integer checksum to be used for the call to krb_mk_req()
  22.  *    and mutual authentication
  23.  *
  24.  * If DEBUG is defined, authenticate to server "test.test".
  25.  */
  26.  
  27. #ifndef    lint
  28. static char rcsid_sample_client_c[] =
  29. "$Header: sample_client.c,v 4.2 89/04/21 13:27:45 jtkohl Exp $";
  30. #endif    lint
  31.  
  32. #include <mit_copy.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <netinet/in.h>
  38. #include <netdb.h>
  39. #include <krb.h>
  40.  
  41. #define SAMPLE_SERVICE    "sample"
  42.  
  43. #ifdef DEBUG
  44. #define    TEST_SERVICE    "test"
  45. #endif
  46.  
  47. #ifdef __BORLANDC__
  48. int _stklen=16000;
  49. #endif
  50.  
  51. extern int krb_debug,krb_ap_req_debug,des_debug;
  52.  
  53. static void close_sockets();
  54. static int sock;
  55.  
  56. main(argc, argv)
  57. int argc;
  58. char **argv;
  59. {
  60.     struct servent *sp;
  61.     struct hostent *hp;
  62.     struct sockaddr_in sin, lsin;
  63.     char *remote_host;
  64.     int status;
  65.     int namelen;
  66.     KTEXT_ST ticket;
  67.     char buf[512];
  68.     long authopts;
  69.     MSG_DAT msg_data;
  70.     CREDENTIALS cred;
  71.     Key_schedule sched;
  72.     long cksum;
  73.     long host_ip;
  74.  
  75.     krb_debug=1;
  76.     krb_ap_req_debug=1;
  77.     des_debug=3;
  78.     if (argc != 3) {
  79.     fprintf(stderr, "usage: %s <hostname> <checksum>\n",argv[0]);
  80.     exit(1);
  81.     }
  82.     
  83.     /* convert cksum to internal rep */
  84.     cksum = (long) atoi(argv[2]);
  85.  
  86.     /* clear out the structure first */
  87.     (void) bzero((char *)&sin, sizeof(sin));
  88.  
  89.     /* find the port number for knetd */
  90.     sp = getservbyname(SAMPLE_SERVICE, "tcp");
  91.     if (!sp) {
  92.     fprintf(stderr,
  93.         "unknown service %s/tcp; check /etc/services\n",
  94.         SAMPLE_SERVICE);
  95.     exit(1);
  96.     }
  97.     /* copy the port number */
  98.     sin.sin_port = sp->s_port;
  99.     sin.sin_family = AF_INET;
  100. #if 1
  101.     /* look up the server host */
  102.     hp = gethostbyname(argv[1]);
  103.     if (!hp) {
  104.     fprintf(stderr, "unknown host %s\n",argv[1]);
  105.     exit(1);
  106.     }
  107.  
  108.     /* copy the hostname into non-volatile storage */
  109.     remote_host = malloc(strlen(hp->h_name) + 1);
  110.     (void) strcpy(remote_host, hp->h_name);
  111.  
  112.     /* set up the address of the foreign socket for connect() */
  113.     sin.sin_family = hp->h_addrtype;
  114.     (void) bcopy((char *)hp->h_addr,
  115.          (char *)&sin.sin_addr,
  116.          hp->h_length);    
  117. #else    /* DNS version */
  118.     printf("Before rhost\n");
  119.     /* copy the hostname into non-volatile storage */
  120.     remote_host = malloc(strlen(argv[1]) + 1);
  121.     (void) strcpy(remote_host, argv[1]);
  122.     host_ip=rhost(&argv[1]);
  123.     sin.sin_family=AF_INET;
  124.     sin.sin_addr.s_addr=host_ip;
  125. #endif
  126.     /* open a TCP socket */
  127.     sock = socket(PF_INET, SOCK_STREAM, 0);
  128.     if (sock < 0) {
  129.         soperror("socket");
  130.     exit(1);
  131.     }
  132.     atexit(close_sockets);
  133.     
  134.     /* connect to the server */
  135.     if (connect(sock,(struct sockaddr*) &sin, sizeof(sin)) < 0) {
  136.         soperror("connect");
  137.     exit(1);
  138.     }
  139.  
  140.     /* find out who I am, now that we are connected and therefore bound */
  141.     namelen = sizeof(lsin);
  142.     if (getsockname(sock, (struct sockaddr *) &lsin, &namelen) < 0) {
  143.         soperror("getsockname");
  144.     exit(1);
  145.     }
  146.  
  147.     /* call Kerberos library routine to obtain an authenticator,
  148.        pass it over the socket to the server, and obtain mutual
  149.        authentication. */
  150.  
  151.     authopts = KOPT_DO_MUTUAL;
  152.     status = krb_sendauth(authopts, sock, &ticket,
  153. #ifdef DEBUG
  154.               TEST_SERVICE, TEST_SERVICE,
  155. #else
  156.               SAMPLE_SERVICE, remote_host,
  157. #endif
  158.               NULL, cksum, &msg_data, &cred,
  159.               sched, &lsin, &sin, "VERSION9");
  160.     if (status != KSUCCESS) {
  161.     fprintf(stderr, "%s: cannot authenticate to server: %s\n",
  162.         argv[0], krb_err_txt[status]);
  163.     exit(1);
  164.     }
  165.  
  166.     /* After we send the authenticator to the server, it will write
  167.        back the name we authenticated to. Read what it has to say. */
  168.     status = soread(sock, buf, 512);
  169.     if (status < 0) {
  170.     perror("read");
  171.     exit(1);
  172.     }
  173.  
  174.     /* make sure it's null terminated before printing */
  175.     if (status < 512)
  176.     buf[status] = '\0';
  177.     printf("The server says:\n%s\n", buf);
  178.  
  179.     exit(0);
  180. }
  181.  
  182. static void close_sockets()
  183. {
  184.     printf("\nClosing socket %d\n",sock);
  185.     soclose(sock);
  186. }
  187.  
  188. void dump(char *hdr,char far*p,int len)
  189. {
  190.     FILE *fp;
  191.     int i;
  192.     
  193.     fp=fopen("dump","a");
  194.     fprintf(fp,"%s\n",hdr);
  195.     for(i=0;i<len;i++,p++) 
  196.         fprintf(fp,"%3d%c%c",*p,isgraph(*p) ? *p : ' ',
  197.             i%40 ? ' ' : '\n');
  198.     fprintf(fp,"\n");
  199. }